package com.example.pingze.service;

import com.example.pingze.entity.CreationUserLove;
import com.example.pingze.exception.PingZeException;
import com.example.pingze.repository.CreationUserLoveRepository;
import com.example.pingze.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class CreationUserLoveService {
    @Autowired
    private CreationUserLoveRepository repository;

    public CreationUserLove add(CreationUserLove creationUserLove) {
        return repository.save(creationUserLove);
    }

    public CreationUserLove findCreationUserLoveByAccountAndCreationId(String account, String creationId) {
        return repository.findCreationUserLoveByAccountAndCreationId(account, creationId);
    }

    public void delete(CreationUserLove love) {
        repository.delete(love);
    }


    public Page getList(CreationUserLove creationUserLove, Pageable pageable) {
        Specification<CreationUserLove> specification = new Specification<CreationUserLove>() {
            @Override
            public Predicate toPredicate(Root<CreationUserLove> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if(!StringUtils.isEmpty(creationUserLove.getId())){
                    predicates.add(criteriaBuilder.equal(root.get("id"),creationUserLove.getId()));
                }
                if(!StringUtils.isEmpty(creationUserLove.getAccount())){
                    predicates.add(criteriaBuilder.equal(root.get("account"),creationUserLove.getAccount()));
                }
                if(!StringUtils.isEmpty(creationUserLove.getCreationId())){
                    predicates.add(criteriaBuilder.equal(root.get("creationId"),creationUserLove.getCreationId()));
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        Page page = repository.findAll(specification, pageable);
        return page;
    }
    public CreationUserLove add(String account ,String creationId){
        if(StringUtils.isEmpty(account)){
            throw new PingZeException("账号不能为空");
        }
        if(StringUtils.isEmpty(creationId)){
            throw new PingZeException("被点赞的创作Id不能为空");
        }
        CreationUserLove add=new CreationUserLove();
        add.setId(UUID.randomUUID().toString());
        add.setAccount(account);
        add.setCreationId(creationId);
        repository.save(add);
        return add;
    }
    public CreationUserLove edit(String id,String account,String creationId){
        CreationUserLove edit = repository.findCreationUserLoveById(id);
        if(!edit.getAccount().equals(account)){
            edit.setAccount(account);
        }
        if(!edit.getCreationId().equals(creationId)){
            edit.setCreationId(creationId);
        }
        repository.save(edit);
        return edit;
    }

    public CreationUserLove findCreationUserLoveById(String id) {
        return repository.findCreationUserLoveById(id);
    }
}
